package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryInWarehouseAdapter
import com.c.ctgproject.adapter.guang.MaterialWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMateiralWarehouseBinding
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.entity.guang.MaterialWarehouseEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerPosition

/**
 *作者:LiBW
 *创建日期: 2021/9/18
 *描述: 物料库存 页面
 */
class MateiralWarehouseActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityMateiralWarehouseBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mList = ArrayList<MaterialWarehouseEntity.DataBean>()
    private val mAdapter by lazy { MaterialWarehouseAdapter(this, mList) }
    private var page=1

    override fun event() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)
    }

    override fun click() {
//        mBinding.mRefresh.setOnLoadMoreListener {
//            page++
//            getData()
//        }
//
//        mBinding.mRefresh.setOnRefreshListener {
//            page = 1
//            getData()
//        }
    }
    private fun getData() {

//        ApiMethods.apiSubscribe(ApiMethods.retrofit.materialWarehouse(page), MyObserverHandlerLoading(this, loading) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.materialWarehouse(), MyObserverHandlerLoading(this, loading) {
            MyLog.d("物料库存回调:$it")
            val data = getGson().fromJson(it, MaterialWarehouseEntity::class.java).data
            data?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mCarRV.handlerPosition(page)

            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        getData()
    }

}